package com.kkb.kkbportal.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kkb.kkbcommon.service.RedisService;
import com.kkb.kkbportal.dao.ScoreLikeCountMapper;
import com.kkb.kkbportal.domain.ScoreLikeCount;
import com.kkb.kkbportal.service.ScoreLikeCountService;
import com.kkb.kkbportal.service.util.RedisKeyEnums;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName ScoreLikeCountServiceImpl
 * @Description 评论获赞
 * @Author syt
 * @Date 2021/08/20
 */
@Service
public class ScoreLikeCountServiceImpl extends ServiceImpl<ScoreLikeCountMapper, ScoreLikeCount> implements ScoreLikeCountService {

    @Autowired
    private RedisService redisService;

    /**
     * 评论获赞计数添加
     *
     * @param scoreId   评论人id
     * @param increment 评论增加或减少的数量
     * @return 修改后的评论数
     */
    @Override
    public Integer countScoreLike(Long scoreId, Integer increment) {

        String scoreIdKey = RedisKeyEnums.SCORE_LIKE_COUNT.getRedisKey() + scoreId;

        // 判断是否存在
        Boolean hasKey = redisService.hasKey(scoreIdKey);

        if (!hasKey) {
            // 不存在，则添加
            redisService.set(scoreIdKey, 0);
        }
        redisService.incr(scoreIdKey, increment);

        return increment;
    }
}
